// pages/guide/index.js
const {
  couponUseList,
  getDictData,
  volunteerPay
} = require('../../api/api')
Page({

  data: {
    tabbarHeight: getApp().globalData.tabbarHeight,
    bottomLift: getApp().globalData.bottomLift,
    imgList: [{
        width: 750,
        height: '',
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/32e72032bd3a478aa778e7088c1149ea.jpg"
      },
      {
        width: 750,
        height: 709,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/6270ce8398884165902e758e4f9006b5.jpg"
      },
      {
        width: 750,
        height: 1438,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/1b4bbe2071924ff6a018e4aefff0269b.jpg"
      },
      {
        width: 750,
        height: 859,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/8abe126b0c4d42b98a4781079c3e654c.jpg"
      },
      {
        width: 750,
        height: 521,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/57a097499cde4291805dca9b080766ae.jpg"
      },
      {
        width: 750,
        height: 1066,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/7c059f9341fc4afaa706f49766b8949c.jpg"
      },
      {
        width: 750,
        height: 1074,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/0515ef28e9fe4a439a7cc57801cc49dd.jpg"
      },
      {
        width: 750,
        height: 1021,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/ea722342573640e0b367c12a5202162b.jpg"
      },
      {
        width: 750,
        height: 932,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/5d9c808f37ac45e892471a13810bc390.jpg"
      },
      {
        width: 750,
        height: 1323,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/79338867ba4d43159459ce0b261e17b9.jpg"
      },
      {
        width: 750,
        height: 1272,
        url: "https://ehsimg.oss-cn-beijing.aliyuncs.com/jeditor/e120fc3f2fe347a3b4adea80e6a8af2e.jpg"
      }
    ],
    banner: [
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/b736b6d5c596460b821882ec6baa478f.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/092160f13a88433a9bbc7805d70e7aba.png',

      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/a938ee0613514fff912fc11be1f27b90.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/8a428b386bbf4429ab8aef6eeb9952ec.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/684fb953d3364607971cc8f1e6faeaaf.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/temp/4d8d9608250e445e96446fb7f85ac026.png'
    ],
    visible: false,
    title: '可用优惠券',
    content: '你有一张优惠券可用',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad(options) {
  //   if (options.params) {
  //     const params = JSON.parse(decodeURIComponent(options.params))
  //     this.setData({
  //       details: params
  //     })
  //   }
  // },
  onLoad(options) {
    this.getImgList()
    this.getBannerList()
  },
  contact(){
    getApp().getCustomerService()
  },
  async getImgList() {
    const {
      result
    } = await getDictData('guide_code')
    var newList = []
    result.map((item) => {
      newList.push(JSON.parse(item.desc))
    })
    this.setData({
      imgList: newList
    })
  },
  async getBannerList() {
    const {
      result
    } = await getDictData('guide_swiper')
    var banner = result.map((item) => {
      return item.value
    })
    this.setData({
      banner
    })
  },
  makePhone(e) {
    const {
      phone
    } = e.currentTarget.dataset
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  confirm(e) {
    const couponId = e.detail
    this.doWxPay(couponId)
  },
  async pay(e) {
    var {
      price,title
    } = e.currentTarget.dataset
    if (price == '' || price == undefined) return
    this.setData({
      vipTitle:title,
      vipPrice:price
    })
    const {
      result
    } = await couponUseList(price)
    if (result.length > 0) {
      this.setData({
        visible: true,
        couponList: result,
        content: '您有' + result.length + '张优惠券可用'
      })
    } else {
      this.doWxPay()
    }
  },
  async doWxPay(couponId) {
    const params = {
      volunteerName: this.data.vipTitle,
      price:this.data.vipPrice
    }
    if (couponId) {
      params.couponId = couponId
    }
    const {
      result
    } = await volunteerPay(params)
    const _this = this
    wx.requestPayment({
      nonceStr: result.nonceStr,
      package: result.package,
      paySign: result.paySign,
      timeStamp: result.timeStamp,
      signType: result.signType,
      success: async function (res) {
        wx.showToast({
          icon: 'success',
          title: '购买成功',
        })
      },
      fail: function (res) {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '志愿指导',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})